package particle_simulator_chem;

import gui.ErrorReportGUI;
import gui.Loader;
import gui.MainGUI;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import auxiliary_classes.Notifier;

import chemistry.Compound;

public class ParticleSimulationInit {
	/**
	 * Launches the Particle Simulator.
	 * 
	 * @param args
	 * @throws Exception 
	 * @throws FileNotFoundException 
	 * @throws UnknownHostException 
	 * @throws SocketException 
	 */
	
	public static void main(String[] args) throws FileNotFoundException, SocketException, UnknownHostException {		
		
		int dimension = 3;
		String version = "V.0.2.7";
		
		//String[] recipients = {"laneaasen@gmail.com", "ghug99@gmail.com", "leahcim165@gmail.com"};
		String[] recipients = {"laneaasen@gmail.com", "ghug99@gmail.com"};
		//String[] recipients = {"laneaasen@gmail.com"};
		Notifier bugNotifier = new Notifier(recipients, "arc.bugreporter@gmail.com", "WF7pk5Fh6Xs5");
		
		int configuration = 0; //0: default, 1: fullscreen, 2: developer suite, 3: fullscreen developer suite (not currently implemented), 4: Mobile version (small)
		int defaultWindowWidth = 764;
		int defaultWindowHeight = 498;
		
		int width = 0;
		int height = 0;
		int depth = 100;
		
		double targetFPS = 60;
		int fpsUpdateRate = 5;
		int fpsResetRate = 3000; //fpsResetRate should be a multiple of fpsUpdateRate to prevent inaccurate results
		
		boolean encircleCursor = false;
		
		ArrayList<Compound> compounds = new ArrayList<Compound>();
		compounds.add(new Compound("H2O"));
		compounds.add(new Compound("Nd2Fe14B"));
		World world = new World(width, height, depth, .01, 300, 10000, 10000, 0.1, compounds);
		
		boolean errorReporting = true;
		File errorFile = null;
		PrintStream errorOut;
		if(errorReporting) {
			try {
				errorFile = File.createTempFile("error", ".txt");
				errorFile.deleteOnExit();
				errorOut = new PrintStream(errorFile);
				//System.setErr(errorOut);
			} catch (IOException e) {
				e.printStackTrace();
				
			}
		}
		
		Shell mainShell = new Shell();
		//mainShell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		mainShell.setImage(SWTResourceManager.getImage(MainGUI.class, "/particle_simulator_chem/BinaryBirdIconV.1.4.png"));
		
		MainGUI window = new MainGUI(
				mainShell, "Particle Simulator 3D V.0.2.7",
				dimension,
				recipients, bugNotifier,
				configuration, defaultWindowWidth, defaultWindowHeight,
				width, height, depth,
				targetFPS, fpsUpdateRate, fpsResetRate,
				encircleCursor,
				world);

		try {
			window.create();
			Image binaryBird = SWTResourceManager.getImage(ParticleSimulationInit.class, "/resources/GrowthOfABoidTransparent.png");
			Loader mainLoader = new Loader(mainShell, mainShell.getStyle(), "Particle Simulator 3D V.0.2.7", SWT.COLOR_WHITE, binaryBird, 10, 500, 500);
			mainLoader.open();
			window.open();
		} catch (Exception e) {
			e.printStackTrace(System.err);
			if(errorReporting) {
				try {
					ErrorReportGUI erg = new ErrorReportGUI(mainShell, mainShell.getStyle(), bugNotifier, "ARC Bug Report", errorFile, version);
					erg.open();
				} catch (Exception e1) {
					e1.printStackTrace(System.out);
				}
			}
		}
	}

//	public static void main(String[] args) throws Exception {
//		Shell shl = new Shell();
//		String[] recipients = {"laneaasen@gmail.com"};
//		Notifier bugNotifier = new Notifier(recipients, "arc.bugreporter@gmail.com", "WF7pk5Fh6Xs5");
//		File errorFile = File.createTempFile("error", ".txt");
//		errorFile.deleteOnExit();
//		PrintStream errorOut = new PrintStream(errorFile);
//		System.setErr(errorOut);
//		String version = "V.0.2.7";
//		ErrorReportGUI erg = new ErrorReportGUI(shl, shl.getStyle(), bugNotifier, "ARC Bug Report", errorFile, version);
//		erg.open();
//	}
}
